;(function(window,$){
	$.fn.extend({
		banner:function(options){
			new Banner(options,this)
		}
	})
	function Banner(obj,ele){
		var defOptions = {
			// 是否循环
			loop:true,
			// 每次动画的时间
			speed:300,
			// 每次轮播的间隔
			time:3000,
			// 是否自动轮播，
			autoPlay:true,
			// 是否有小圆点
			isDot:true,
			// 默认显示第几张
			index:0,
			// 是否有左右按钮
			isLeftAndRight:true
		}
		var opt = $.extend({},defOptions,obj)
		this.$ele = ele;

		this.loop = opt.loop;
		this.speed = opt.speed ;
		this.time = opt.time;
		this.autoPlay = opt.autoPlay;
		this.isDot = opt.isDot;
		this.index = opt.index;
		this.isLeftAndRight = opt.isLeftAndRight;

		this.width = ele.find(".img li").width()
		this.timer = null;
		this.initDOM()
		
	}
	Banner.prototype.initDOM = function(){
		var that = this;
		if(this.isDot){
			$.each(this.$ele.find("li"),function(){
				that.$ele.find("ul.num").append("<li></li>")
			})
			that.$ele.find("ul.num li").eq(that.index).addClass('active')
		}
		if(this.loop){
			var first = this.$ele.find("ul.img li").first().clone()
			var last = this.$ele.find("ul.img li").last().clone()
			this.$ele.find("ul.img").append(first).prepend(last).css("width",this.$ele.find("ul.img li").length*this.width);
		}
		if(this.isLeftAndRight){
			this.$ele.hover(function(){
				that.$ele.find(".btn").show()
				clearInterval(that.timer)
			},function(){
				that.$ele.find(".btn").hide()
				that.timer = setInterval(run, that.time)
			})
		}
		if(this.autoPlay){
			this.timer = setInterval(run, this.time)
		}
		this.$ele.find(".next").click(function(){
			run()
		})
		this.$ele.find(".prev").click(function(){
			if(that.$ele.find(".img").is(":animated")){
	    		return 
	    	}
        	that.index--
        	that.$ele.find(".img").animate({left:"+=" + that.width},that.speed,function(){
        		if(that.index == -1){
        			that.index = $(".num li").length - 1
        			that.$ele.find(".img").css("left",-that.width * that.$ele.find(".num li").length)
        		}
        		that.$ele.find(".num li").eq(that.index).addClass('active').siblings().removeClass('active')
        	});
		})
		function run(){
			var self = that
			if(self.$ele.find(".img").is(":animated")){
	    		return 
	    	}
	    	self.index++
	    	self.$ele.find(".img").animate({left:"-=" + self.width},self.speed,function(){
	    		if(self.index == self.$ele.find(".num li").length){
	    			self.index = 0
	    			self.$ele.find(".img").css("left",-self.width)
	    		}
	    		self.$ele.find(".num li").eq(self.index).addClass('active').siblings().removeClass('active')
	    	});
		}
	}
})(window,jQuery)
